/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.compiere.apps.form;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import org.compiere.apps.form.z_invoiceList;
import org.compiere.model.Mzuser;
import org.compiere.apps.form.zutils;

import com.ziclix.python.sql.JDBC20DataHandler;

/**
 *
 * @author alm
 */
public class zlogin extends javax.swing.JDialog {
    z_invoiceList parent = null;
    /**
     * Creates new form NewJDialog
     */
    public zlogin(FormPanel parent, boolean modal) {
        super(new JFrame(), modal);
        
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(zlogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(zlogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(zlogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(zlogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        
        initComponents();
        this.parent = (z_invoiceList) parent;
        
        // make the frame half the height and width
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) ((dimension.getWidth() - this.getWidth()) / 2);
        int y = (int) ((dimension.getHeight() - this.getHeight()) / 2);
        this.setLocation(x, y);        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jLayeredPane1 = new javax.swing.JLayeredPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfIIN = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jpfPin = new javax.swing.JPasswordField();
        jLabel4 = new javax.swing.JLabel();
        jtfCert = new javax.swing.JTextField();
        jbChooseFile = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jbEnter = new javax.swing.JButton();
        jbCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(254, 254, 254));
        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel5.setText("Авторизация");

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel1.setText("Введите следующие данные для входа в личный кабинет");

        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("images/1357291692_Login.png"))); // NOI18N
//        File file = new File("/home/alm/Documents/work/trunk/zerde/src/org/compiere/apps/form/images/1357291692_Login.png");
//        try {
//			System.out.println(file.getCanonicalPath());
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//        jLabel6.setIcon(new javax.swing.ImageIcon("/form/images/cloud.png")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel1)))
                .addGap(0, 29, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addGap(24, 24, 24))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(0, 11, Short.MAX_VALUE)
                .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel1.setBounds(1, 1, 452, 110);
        jLayeredPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel2.setText("БИН");
        jLabel2.setBounds(90, 130, 21, 13);
        jLayeredPane1.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jtfIIN.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jtfIIN.setBounds(120, 120, 286, 23);
        jtfIIN.addKeyListener(new KeyAdapter() {  
            public void keyTyped(KeyEvent e) {  
              char c = e.getKeyChar();  
              if (!(Character.isDigit(c) ||  
                 (c == KeyEvent.VK_BACK_SPACE) ||  
                 (c == KeyEvent.VK_DELETE))) {  
                   e.consume();  
                 }
            	if(!jpfPin.getText().equals("") && !jtfIIN.getText().equals("")){
              		jbEnter.setEnabled(true);
                    if(c == KeyEvent.VK_ENTER)
                   	 jbEnterActionPerformed(null);

              	}
              	else
              		jbEnter.setEnabled(false);
           	}
 
          });         
        
        jLayeredPane1.add(jtfIIN, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel3.setText("Пин");
        jLabel3.setBounds(90, 160, 19, 13);
        jLayeredPane1.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jpfPin.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jpfPin.setBounds(120, 150, 142, 23);
        jpfPin.addKeyListener(new KeyAdapter() {  
            public void keyTyped(KeyEvent e) {  
              char c = e.getKeyChar();  
              if (!(Character.isDigit(c) ||  
                 (c == KeyEvent.VK_BACK_SPACE) ||  
                 (c == KeyEvent.VK_DELETE))) {  
                   e.consume();  
                 }
              
          	if(!jpfPin.getText().equals("") && !jtfIIN.getText().equals("")){
          		jbEnter.setEnabled(true);
                if(c == KeyEvent.VK_ENTER)
               	 jbEnterActionPerformed(null);

          	}
          	else
          		jbEnter.setEnabled(false);
          	}
  
          });        
        jLayeredPane1.add(jpfPin, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jLabel4.setText("Цифровая подпись");
        jLabel4.setBounds(20, 188, 91, 13);
        jLayeredPane1.add(jLabel4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jtfCert.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jtfCert.setBounds(120, 180, 200, 23);
        jLayeredPane1.add(jtfCert, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jbChooseFile.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbChooseFile.setText("Выберите файл");
        jbChooseFile.setBounds(327, 180, 110, 25);
        jbChooseFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	jbChooseFileActionPerformed(evt);
            }
        });
        jLayeredPane1.add(jbChooseFile, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jPanel2.setBackground(new java.awt.Color(254, 254, 254));
        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jbEnter.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbEnter.setText("Вход");
        jbEnter.setEnabled(false);
        jbEnter.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbEnterActionPerformed(evt);
            }
        });

        jbCancel.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        jbCancel.setText("Отмена");
        jbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(288, Short.MAX_VALUE)
                .addComponent(jbEnter, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCancel)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbEnter)
                    .addComponent(jbCancel))
                .addContainerGap())
        );

        jPanel2.setBounds(1, 216, 452, 60);
        jLayeredPane1.add(jPanel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 453, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 276, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();    }// </editor-fold>                        

    private void jbEnterActionPerformed(java.awt.event.ActionEvent evt) {   
    	
    	Mzuser zuser = Mzuser.login(jtfIIN.getText(), jpfPin.getText()); 
    	if(zuser != null){
	    	parent.SetL(zuser);
	    	dispose();
    	}else{
    		JOptionPane.showMessageDialog(this, "Ошибочная комбинация: ИИН и Пин", "Аутентификация", JOptionPane.ERROR_MESSAGE, null);
    	}
    }                                        

    
    private void jbChooseFileActionPerformed(java.awt.event.ActionEvent evt) {
    	final JFileChooser fc = new JFileChooser();
    	fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		if (JFileChooser.APPROVE_OPTION == fc.showOpenDialog(this)) {
			String file = fc.getSelectedFile().getAbsolutePath();
			jtfCert.setText(file);
		}else{
			jtfCert.setText("");
		}
    }
    
    private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {   
    	parent.SetL(null);
    	dispose();
    }                      
    
    private void jftfIINKeyPressed(java.awt.event.KeyEvent evt) {
    	if(!jpfPin.getText().equals("") && !jtfIIN.getText().equals("")){
    		jbEnter.setEnabled(true);

    		if(evt.getKeyCode() == 13)
        		jbEnterActionPerformed(null);

    	}
    	else
    		jbEnter.setEnabled(false);
    	
    		
    }
    
    private void jtfPassKeyPressed(java.awt.event.KeyEvent evt) {
    	if(!jpfPin.getText().equals("") && !jtfIIN.getText().equals("")){
    		jbEnter.setEnabled(true);
    	}
    	else
    		jbEnter.setEnabled(false);
    }    
    
    private void jtfCertKeyPressed(java.awt.event.KeyEvent evt) {
         
    }    
    
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify                     
    // Variables declaration - do not modify
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbChooseFile;
    private javax.swing.JButton jbEnter;
    private javax.swing.JTextField jtfCert;
    private javax.swing.JPasswordField jpfPin;
    private javax.swing.JTextField jtfIIN;
    // End of variables declaration    // End of variables declaration                   
}
